import { useCreateModal } from '@/hooks'
import EditModal from './edit-modal.vue'
import { createVNode } from 'vue'
import { firewallDelete } from '@/api/strategy/firewall'
import span from '@/utils/span-solt.vue'
import { message, Modal } from 'ant-design-vue'
import { QuestionCircleOutlined } from '@ant-design/icons-vue'
import { checkStatus } from '@/utils/http/axios/checkStatus'

export const columns = [
  // 进程策略
  {
    title: '策略名称',
    dataIndex: 'name'
  },
  {
    title: '协议',
    dataIndex: 'protocol',
    slots: {
      customRender: 'protocol'
    },
    slotsType: 'format',
    slotsFunc: val =>
      ({
        0: 'TCP',
        1: 'UDP',
        2: 'TCP,UDP'
      }[val])
  },
  {
    title: '本地端口区间',
    dataIndex: 'lports',
    slots: {
      customRender: 'lports'
    },
    slotsType: 'component',
    slotsFunc: record => createVNode(span, { text: record.localport + '~' + record.localendport })
  },
  {
    title: '远程地址区间',
    dataIndex: 'ipstr',
    slots: {
      customRender: 'ipstr'
    },
    slotsType: 'component',
    slotsFunc: record =>
      createVNode(span, { text: record.externalipstr + '~' + record.externalendipstr })
  },
  // {
  //     title: '远程端口区间',
  //     dataIndex: 'exports',
  //     slots: {
  //         customRender: 'exports'
  //     },
  //     slotsType: 'component',
  //     slotsFunc: (record) => createVNode("span", {}, record.externalport + "~" + record.externalendport)
  // },
  {
    title: '是否禁止',
    dataIndex: 'using',
    slots: {
      customRender: 'using'
    },
    slotsType: 'format',
    slotsFunc: val =>
      ({
        1: '是',
        0: '否'
      }[val])
  },
  // {
  //   title: "应用模板名称",
  //   dataIndex: "tempname"
  // },
  {
    title: '操作',
    dataIndex: 'action',
    width: 200,
    slots: {
      customRender: 'action'
    },
    actions: [
      {
        type: 'popconfirm', // 控制类型，默认为a,可选： select | button | text
        key: 'fileid', // 删除的依据，如果需要根据多个字段删除，则字段之间以英文逗号分隔开，例如： id, name
        text: '删除',
        permission: 'firewall:delete',
        props: {
          type: 'danger'
        },
        func: ({ record }, callback) => {
          firewallDelete({ ids: [record.id], force: 0 }, { isShowMessage: false }).then(res => {
            if (-5 == Number(res.code)) {
              Modal.confirm({
                title: '提示',
                icon: createVNode(QuestionCircleOutlined),
                content: '策略已在模板中使用，是否解除所有绑定并删除',
                onOk: async () => {
                  await firewallDelete({ ids: [record.id], force: 1 }, { isShowMessage: true })
                  callback()
                }
              })
            } else {
              if (0 == Number(res.code)) {
                message.success(res.message)
              } else {
                checkStatus(res.code, res.message, true)
              }
              callback()
              return Promise.resolve()
            }
          })
        }
      },
      {
        type: 'button', // 控制类型，默认为a,可选： select | button | text
        key: 'fileid', // 删除的依据，如果需要根据多个字段删除，则字段之间以英文逗号分隔开，例如： id, name
        text: '编辑',
        permission: 'firewall:update',
        props: {
          type: 'warning'
        },
        func: ({ record }, callback) =>
          useCreateModal(EditModal, {
            fields: record,
            callback: callback
          })
      }
    ]
  }
]
